package com.view.adminMenu;

import com.run.UserRun;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class addUser {
    private JLabel info = new JLabel("请填写新添加用户信息:");
    private JLabel usernameLabel = new JLabel("账号:");
    private JLabel passwordLabel = new JLabel("密码:");
    private JLabel identityLabel = new JLabel("身份:");
    private JLabel IDLabel = new JLabel("ID:");
    private JTextField usernameField = new JTextField(20);
    private JTextField passwordField = new JTextField(20);
    private JTextField IDField = new JTextField(20);
    private JRadioButton identity1 = new JRadioButton("user");
    private JRadioButton identity2 = new JRadioButton("manager");
    private ButtonGroup group = new ButtonGroup();
    private JButton button = new JButton("确定");
    private Box usernameBox = Box.createHorizontalBox();
    private Box passwordBox = Box.createHorizontalBox();
    private Box identityBox = Box.createHorizontalBox();
    private Box IDBox = Box.createHorizontalBox();
    private Box mainBox = Box.createVerticalBox();

    public JPanel init() {
        JPanel mainPanel = new JPanel();

        group.add(identity1);
        group.add(identity2);

        usernameBox.add(usernameLabel);
        usernameBox.add(Box.createHorizontalStrut(20));
        usernameBox.add(usernameField);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(passwordField);

        identityBox.add(identityLabel);
        identityBox.add(Box.createHorizontalStrut(20));
        identityBox.add(identity1);
        identityBox.add(identity2);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(20));
        IDBox.add(IDField);

        mainBox.add(Box.createVerticalStrut(100));
        mainBox.add(info);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(usernameBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(passwordBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(IDBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(identityBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(button);

        mainPanel.add(mainBox);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newUsername = usernameField.getText();
                String newPassword = passwordField.getText();
                String newIdentity = null;
                String ID = IDField.getText();
                if(identity1.isSelected()) {
                    newIdentity = "user";
                }
                else if(identity2.isSelected()) {
                    newIdentity = "manager";
                }
                Connection conn = null;
                try {
                    conn = com.util.DBUtil.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                try {
                    Boolean isAdded = new UserRun().addUser(conn,newUsername,newPassword,newIdentity,ID);
                    if(isAdded = true) {
                        JOptionPane.showMessageDialog(null,"用户已添加");
                    }
                    else {
                        JOptionPane.showMessageDialog(null,"用户添加失败");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        return mainPanel;
    }


}
